<template>
  <div>
    <!-- 添加 -->
    <el-button size="small" @click="toAddHandler">添加</el-button>

    <!-- 表格 -->
    <el-table :data="privilege" style="width: 100%;margin-bottom: 20px;" size="small" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="route" label="路径" />
      <el-table-column prop="type" label="类型" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <span @click="deleteHandler(scope.row.id)">移除</span>
          <span @click="editHandler(scope.row)">修改</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.route" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.type" placeholder="请选择" clearable>
            <el-option label="菜单" value="menu" />
            <el-option label="方法" value="method" />
          </el-select>
        </el-form-item>
        <el-form-item label="父权限">
          <el-cascader v-model="form.parentId" :options="privilege" :props="{ checkStrictly: true ,children: 'children', label: 'name', value: 'id'}" clearable />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.telephone" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveHandler">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs'
export default {
  data() {
    return {
      privilege: [],
      dialogVisible: false,
      form: {},
      title: ''
    }
  },
  created() {
    this.findAllPrivilieges()
  },
  methods: {
    // 删除
    deleteHandler(id) {
      console.log(id)
      request.delete('/privilege/deleteById', { params: { id }}).then(res => {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.findAllPrivilieges()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      })
    },
    // 修改
    editHandler(row) {
      this.form = row
      this.title = '修改权限'
      this.dialogVisible = true
    },
    // 保存
    saveHandler() {
      if (this.form.parentId == null) {
        this.form.parentId = ''
      } else {
        if (typeof (this.form.parentId) === 'object') {
          this.form.parentId = this.form.parentId[0].toString()
        } else {
          this.form.parentId = this.form.parentId
        }
      }

      request.post('/privilege/saveOrUpdate', qs.stringify(this.form)).then(res => {
        this.dialogVisible = false
        this.findAllPrivilieges()
        console.log(res)
      })
    },
    // 添加
    toAddHandler() {
      this.dialogVisible = true
      this.title = '添加权限'
    },
    // 关闭添加模态框
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 查询所有权限
    findAllPrivilieges() {
      request.get('/privilege/findPrivilegeTree').then(res => {
        // this.privilege = res.data
        // 调用递归方法，去除级联数据后将数据赋值给级联选择器
        this.privilege = this.getTreeData(res.data)
      })
    },
    // 递归方法
    getTreeData(data) {
      // 循环遍历json数据
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i].children = undefined
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i].children)
        }
      }
      return data
    }
  }
}
</script>

<style scoped>
    span {
        margin-right: .3rem;
        cursor: pointer;
    }
</style>
